<?php
class PaycallbackControl extends Control{
	function __construct(){
		parent::__construct();
		$this->user_id=$_SESSION["user_info"]["user_id"];
        load_model("charge_order");
        $this->model=new Charge_orderModel();
        $this->time=time();
	}

	function agri_result(){
         
        $msg = base64_decode($_POST['MSG']);
        preg_match("/\<Message\>(.*)\<\/Message\>.*\<Signature\>(.*)\<\/Signature\>/i",$msg,$match);
        $contents = $match[1];
        $signature = $match[2];
         
        $pub_cert_file = UPATH . '/library/payments/agri/TrustPay.pem';
        $rs=$this->_verify($contents,$signature,$pub_cert_file );
        $xml = new Xml();
        $msg = $xml->xml2array($msg);
        $statu=$msg["MSG"]["Message"]["TrxResponse"]["ReturnCode"];
        $order_sn_main=$msg["MSG"]["Message"]["TrxResponse"]["OrderNo"];
        $money=$msg["MSG"]["Message"]["TrxResponse"]["Amount"];
        
        if($rs && ($statu=="0000")){
        	   $rs=$this->model->_dao->query("select * from tcz_charge_order where serial_number='{$order_sn_main}' and user_id={$this->user_id}");
                $order=$rs->row;
        		$this->model->begin_transaction();
                if(!$this->set_order_status($order_sn_main,1)){$this->model->rollback_transaction();}
	            if(!$this->pay_record($order_sn_main,$money,$order)){$this->model->rollback_transaction();}
	           if(!$this->bhb_record($order_sn_main,$money,$order)){$this->model->rollback_transaction();}
               if(!$this->bhb_amount($money)){$this->model->rollback_transaction();}
               if(!$this->update_paysign($order_sn_main)){$this->model->rollback_transaction();}
               $this->model->commit_transaction();
	            header("Location:".SITE_URL."/index.php?app=shop.cart&act=orderSuccessed&order_sn={$order_sn_main}");
        	
        }        
        exit();

    }



    function _verify($data,$signature,$cert_file){
        $fp = fopen($cert_file, "r");
        $cert = fread($fp, 8192);
        fclose($fp);
        $pubkeyid = openssl_get_publickey($cert);
        $signature = base64_decode($signature);
        // state whether signature is okay or not
        $ok = openssl_verify($data, $signature, $pubkeyid);
        openssl_free_key($pubkeyid);
        return $ok;
    }


    //支付成功，更改订单状态，插入一条充值记录，设置余额
    function set_order_status($order_sn,$status){
        
        return $this->model->_dao->query("upadte tcz_charge_order set status=1,pay_time={$this->time} where user_id={$this->user_id} and serial_number='{$order_sn}'");
    }

    //插入一条支付记录
    function pay_record($order_sn,$money,$order){
        
        return $this->model->_dao->query("insert into tcz_order_pay set order_id={$order['order_id']},order_sn_main='{$order_sn}',payment_id={$order['pay_type']},money={$money},pay_time={$this->time},status='succ'");
    }

    //插入博货宝增长记录
    function bhb_record($order_sn,$money,$order){
        return $this->model->_dao->query("insert into tcz_member_vcount set user_id={$this->user_id},order_id={$order['order_id']},order_sn_main={$order_sn},countvalue={$money},source=3,add_time={$this->time},operate_id=0,note='博货宝充值'");
    }

    //设置博货宝账户余额
    function bhb_amount($money){
        return $this->model->_dao("update tcz_member set vcount=vcount+{$moeny} where user_id={$this->user_id}");
    }

    //更改支付记录状态
    function update_paysign($order_sn){
        return $this->model->_dao("update tcz_order_paysign set status='succ',ftime=>{$this->time} where order_sn_main={$order_sn}");
    }
}
?>